<template>
    <div class="inspectorLibrary-increase">
        <!-- 新增弹窗 -->
        <a-modal
         width="100%"
         wrapClassName="full-modal"
         class="inspectorLibrary-modal"
         :visible="visibleShow" 
         :title="addEditParameter.id?'编辑':'新增'" 
         @ok="handleOk" 
         :forceRender="true"
         :confirmLoading="confirmLoading"
         @cancel="visibleShow = false">
            <userMessage 
            :key="userMessageKey" 
            :inspectorId="{id:addEditParameter.id,fuserid:addEditParameter.fuserid}" 
            @handleOk="seveHandleOk"
            />
        </a-modal>
    </div>
</template>
<script>
import { ref } from 'vue';
import {typeInput, typeNumber, typeSelect, typeCheckbox, typeRadio} from '@/components/compSearchCriteria/ContractType'
import compSelector from '@/components/compSelector/compSelector'
import validator from '@/common/validator'
import {addServe, editServe, ocalityPostAction, modifyServe} from './service/index'
import moment from 'moment'
import blobType from '@/common/blobType';
import assessCheckApi from './service/api'
import userMessage from '../../everydayCheck/inspectormessage/UserMessage.vue'
export default {
    components:{
        compSelector,
        userMessage
    },
    props:{
        addEditParameter:{
            type:Object,
            default:[]
        }
    },
    data(){
        return{
            userMessageKey:null,
            showValue:'',
            showValue1:'',
            assessCheckApi:assessCheckApi,
            blobType:blobType,
            routeName:this.$route.name,
            typeRadio:typeRadio,
            typeSelect:typeSelect,
            form: this.$form.createForm(this, { name: 'coordinated' }),
            visibleShow:false,
            confirmLoading:false,
            typeSelect:typeSelect,
            formState:{
                fywflcode:blobType.fywflcodeArr[blobType.menuType.indexOf(this.$route.name.substring(this.$route.name.indexOf('__'),this.$route.name.indexOf('_a')+2))],
                fuserid:'',
                username:'',
                id:'',
                realname:'',
            },
            rules:{
                username:[{
                    required: true,
                    message: '请输入账号',
                    trigger: 'blur',
                }],
                realname:[{
                    required: true,
                    message: '请输入真实姓名',
                    trigger: 'blur',
                }],
                fsfzh:[{
                    required: true,
                    message: '请输入身份证号',
                    trigger: 'blur',
                },{
                    pattern: validator.isIdCard.reg,
                    message: '请输入正确的身份证号',
                    trigger: 'blur',
                }],
                phone: [{
                    required: true,
                    message: '请输入手机号码',
                    trigger: 'blur',
                },{
                    pattern: validator.isPhone.reg,
                    message: '请输入正确的手机号码',
                    trigger: 'blur',
                }],
                // email:[{
                //     required: true,
                //     message: '请输入电子邮箱',
                //     trigger: 'blur',
                // },{
                //     pattern: validator.isEmail.reg,
                //     message: '请输入正确的电子邮箱',
                //     trigger: 'blur',
                // }],
                fgzdw:[{
                    required: true,
                    message: '请输入单位名称',
                    trigger: 'blur',
                }],
            },
            formRequer:'',
            optionsArr:[],
            submittedSuccessfully:false,
            editInterfaceInfo:{
                title:'',
                data:{
                    data:{
                       id:'' 
                    }
                },
                key:'data',
                dataKey:'id',
                dictionaryType:''
            },
            separateVisible:false,
            separateKey:null,
        }
    },
    watch:{
        addEditParameter(newval,oldVal){
            this.userMessageKey = Date.now()
            //console.log('给了什么啊',newval)
            this.formState = {...this.formState,...newval}
            this.formState.fspsj = newval.fspsj?moment(newval.fspsj).format('YYYY-MM-DD'):''
            this.formState.fspjssj = newval.fspjssj?moment(newval.fspjssj).format('YYYY-MM-DD'):''
        }
    },
    mounted(){
        ocalityPostAction({dictName:'观察员所属地市'}).then(res=>{
                //console.log('字典的res',res)
                if(res.code == 200){
                    if(res.result.length){
                        this.optionsDataChange(res.result,{label:'itemText',value:'itemValue'})
                    }
                    
                }
            })
    },
    methods:{
        optionsDataChange(dataList,dataType){
            var newOptionArr = []
            //标识每一项的下标，并且添加到每一项
            dataList.forEach((item,index) => {
                 
                //console.log(newOptionArr.filter(l=>l.itemIndex === index))
                if(!newOptionArr.filter(l=>l.itemIndex === index).length){
                    newOptionArr.push({...item,itemIndex:index})
                }
            });
            //console.log('这是有下标的数组',newOptionArr)
            for (let i = 0; i <= newOptionArr.length; i++) {//打印的item为数组中的每一个对象
                let item = newOptionArr[i]
                let index = i
                for (let key in item) { //遍历数组中的对象
                    var currentItem = {}
                    //console.log(index)
                     
                    if(index === item.itemIndex){
                        currentItem = {
                            label:item[dataType.label],
                            value:item[dataType.value]
                        }
                        
                        if(!this.optionsArr.filter(l=>l.label === currentItem.label).length && currentItem.label){
                            this.optionsArr.push(currentItem)
                        }
                    }
                    

                }
            }
            //console.log('当前获取到的optionArr',this.optionsArr)
        },
        forderChange(val){
            //console.log(val)
            if(!val){
                this.formState.forder = 1
            }
        },
        handleSubmit(e) {
            // e.preventDefault();
            var formState = {}
            this.form.validateFields((err, values) => {
                if (!err) {
                    this.formState.sex = this.formState.sex.value?this.formState.sex.value:this.formState.sex
                    this.formState.fgzzt = this.formState.fgzzt.value?this.formState.fgzzt.value:this.formState.fgzzt
                    this.formState.fssk = this.formState.fssk.value?this.formState.fssk.value:this.formState.fssk
                    this.formState.fstate = this.formState.fstate.value?this.formState.fstate.value:this.formState.fstate
                    this.formState.fusertype = this.formState.fusertype.value?this.formState.fusertype.value:this.formState.fusertype
                    this.confirmLoading = true
                    this.formState.fspsj = this.formState.fspsj? moment(this.formState.fspsj).format('YYYY-MM-DD'):'' 
                    this.formState.fspjssj = this.formState.fspjssj? moment(this.formState.fspjssj).format('YYYY-MM-DD'):''
                    var userMap = {
                        sex:this.formState.sex,
                        fuserid:this.formState.fuserid,
                        username:values.username,
                        realname:values.realname,
                        fsfzh:this.formState.fsfzh,
                        phone:values.phone,
                        email:this.formState.email,
                        fssqy:this.formState.fssqy,
                        fmz:this.formState.fmz,
                        fzzmm:this.formState.fzzmm,
                        fxl:this.formState.fxl,
                        fbyyx:this.formState.fbyyx,
                        fsxzy:this.formState.fsxzy,
                        fcsgz:this.formState.fcsgz,
                        fzc:this.formState.fzc,
                        fzw:this.formState.fzw,
                        fgzdw:values.fgzdw,
                        fgzdwdz:this.formState.fgzdwdz,
                        fdwdh:this.formState.fdwdh,
                        fdwyx:this.formState.fdwyx,
                        fgzzt:this.formState.fgzzt,
                        fyhkh:this.formState.fyhkh,
                        fkhhszd:this.formState.fkhhszd,
                        fkfh:this.formState.fkfh,
                        fdwlx:this.formState.fdwlx,
                        fsfyjsry:this.formState.fsfyjsry,
                    }
                    var hcyMap = {
                        fywflcode:this.formState.fywflcode,
                        id:this.formState.id,
                        fspsj:this.formState.fspsj,
                        fspjssj:this.formState.fspjssj,
                        fszly:this.formState.fszly,
                        flevel:this.formState.flevel,
                        frkpc:this.formState.frkpc,
                        fssk:this.formState.fssk,
                        fstate:this.formState.fstate,
                        fusertype:this.formState.fusertype,
                        fzsbh:this.formState.fzsbh,
                        // fzylx1:this.formState.fzylx1,
                        // fzylx2:this.formState.fzylx2,
                        // fzylx3:this.formState.fzylx3,
                        
                        fpywjh:this.formState.fpywjh,
                        fxllx:this.formState.fxllx,
                        flx:this.formState.flx,
                        fjzqk:this.formState.fjzqk,
                        fjcyzg:this.formState.fjcyzg,
                        fssjcjg:this.formState.fssjcjg,
                    }
                    if(this.formState.id){
                        editServe({hcyMap:hcyMap,userMap:userMap}).then(res=>{
                            if(res.code == 200){
                                this.submittedSuccessfully = true
                                this.$message.success('修改成功')
                                this.confirmLoading = false
                                this.visibleShow = false
                                this.$emit('handleOk',true,this.formState.id)
                            }else{
                                this.$message.warning(res.message)
                                this.confirmLoading = false
                            }
                        })
                    }else{
                        addServe({hcyMap:hcyMap,userMap:userMap}).then(res=>{
                            if(res.code == 200){
                                this.$message.success('添加成功')
                                
                                this.submittedSuccessfully = true
                                this.confirmLoading = false
                                this.visibleShow = false
                                this.$emit('handleOk',true,'')
                            }else{
                                this.$message.warning(res.message)
                                this.confirmLoading = false
                            }
                        })
                    }
                    
                }
            });
            
        },
        seveHandleOk(){
            this.$emit('handleOk',true,1)
        },
        handleOk(){
            this.visibleShow = false
            // this.handleSubmit()
        }
    }

}

</script>
<style lang="less">
/* .ant-form-item-label{
    font-weight: 600;
} */

 .ant-modal-title{
    font-weight: 600;
 }
 .ant-modal-header{
    background: #5ca3e5;
 }
 .ant-upload-list{
    display: none !important;
 }
 .ant-modal-footer .ant-btn-primary{
    border: 1px solid #5ca3e5 !important;
    background-color: #5ca3e5 !important;
    color: #fff !important;
 }
//  .inspectorLibrary-modal{
//     .ant-form-item-label{
//         width: 120px !important;
//     }
//     // .ant-form-item-control-wrapper{
//     //     width: 150px !important;
//     // }
//  }
//  .inspector-library-item-box{
//     .ant-form-item-control-wrapper{
//         width: 436px !important;
//     }
// }
 
</style>
<style lang="less" scoped>
.full-modal {
  .ant-modal {
    max-width: 100%;
    top: 0;
    padding-bottom: 0;
    margin: 0;
  }
  .ant-modal-content {
    display: flex;
    flex-direction: column;
    height: calc(100vh);
  }
  .ant-modal-body {
    flex: 1;
    background: #fff !important;
  }
  .ant-modal-footer{
    background: #fff !important;
  }
}
</style>